import random
import time
import threading

# def coding():
#     print(threading.current_thread()) # 当前线程
#     print(threading.current_thread().name)
#     for i in range(3):
#         print('正在写代码')
#         time.sleep(1)
#
#
# def drawing():
#     print(threading.current_thread())
#     print(threading.current_thread().name)
#     for i in range(3):
#         print('正在写画图')
#         time.sleep(1)
#
#
# def mu():
#
#     threading1 = threading.Thread(target=coding,name='张三')
#     threading2= threading.Thread(target=drawing,name='李四')
#
#     threading1.start()
#     threading2.start()
#
#
#     print(threading.enumerate())  #_MainThread:主线程
#
# if __name__ == '__main__':
#     mu()



# =========================================================================

# class CodingThread(threading.Thread):
#
#     def run(self):
#         print(threading.current_thread()) # 当前线程对象
#         print(threading.current_thread().name)  # 当前线程名字
#         for i in range(3):
#             print('正在写代码')
#             time.sleep(1)
#
# class DrawingThread(threading.Thread):
#
#     def drawing(self):
#         print(threading.current_thread())
#         print(threading.current_thread().name)
#         for i in range(3):
#             print('正在写画图')
#             time.sleep(1)
#
#
# def mu():
#
#     threading1 = CodingThread()
#     threading2= DrawingThread()
#
#     threading1.start()
#     threading2.start()
#
#
#     # print(threading.enumerate())  #_MainThread:主线程
#
# if __name__ == '__main__':
#     mu()










# =============================================
# 锁

# values = 0
#
# gLock = threading.Lock()
#
# def add_value():
#     global values
#     gLock.acquire() # 上锁  把尽量少的和不耗时的代码放到锁中执行
#     for i in range(1000000):
#         values +=1
#
#     gLock.release()  # 释放锁  代码执行完成后要记得释放锁
#     print(values)
#
# def main():
#
#     for x in range(2):
#         th = threading.Thread(target=add_value)
#         th.start()
#
# if __name__ == '__main__':
#     main()



# ======================================
# 生产者和消费者
gmoney = 0

gCondition = threading.Condition()

gtime = 0

class Producer(threading.Thread):


    def run(self):
        global gmoney,gtime
        while True:
            gCondition.acquire()
            if gtime >= 10:
                gCondition.release()
                break
            money = random.randint(0, 100)
            gmoney += money
            gtime += 1
            print('生产者%s' % (threading.current_thread().name),money)
            gCondition.release()

class Consumer(threading.Thread):

    def run(self):
        global gmoney,gtime
        while True:
            gCondition.acquire()
            money = random.randint(0, 100)
            if gmoney >= money:
                gmoney -= money
                print('消费者%s' % (threading.current_thread().name), money)
            else:
                if gtime >= 10:
                    gCondition.release()
                    break
                print('余额不足',(threading.current_thread().name), money)
            gCondition.release()

def main():
    for i in range(5):
        th = Producer(name='生产者%d' % i)
        th.start()

    for i in range(5):
        th = Consumer(name='消费者%d' % i)
        th.start()



if __name__ == '__main__':
    main()